var xmlHttp;
var randomfolder;
var datepath;
var allowtype;
function createXmlHttp(){
	if(window.XMLHttpRequest){
		xmlHttp = new XMLHttpRequest();
	}else{
	    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function initAttach(path,displayname) {
	if(displayname==null || displayname=="") {
		displayname = path.substring(path.lastIndexOf("/")+1);
	}
	if(path==null || path=="" || path.indexOf("serverfiles")>=0) {
		return false;
	}else {
		createXmlHttp();
		randomfolder = document.getElementById("randomfolder").value;
		xmlHttp.onreadystatechange = showUploadFile;
		xmlHttp.open("POST", "upload-initAttach.action", true);
    	xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    	xmlHttp.send("randomfolder="+randomfolder+"&displayname="+displayname+"&editFileAttachPath="+path+"&timestamp="+new Date().getTime());
	}
}

function addFile(){
   i=0;
   sendRequest("ajaxControl=addFile&randomfolder="+randomfolder+"&");
}

function doSubmit(url){
	allowtype = document.getElementById("allowtype").value;
	var attachnum = document.getElementById("attachnum").value;
	randomfolder = document.getElementById("randomfolder").value;
	datepath = document.getElementById("datepath").value;
	var thisForm = document.uploadForm;
	thisForm.action = "upload-iframeRequest.action?url="+encodeURI(url)+"&attachNum="+attachnum+"&randomfolder="+randomfolder+"&datepath="+datepath+"&allowType="+allowtype;
	thisForm.submit();
    startStatusCheck();
}
function emptyFile() {
    sendRequest("ajaxControl=emptyFile&randomfolder="+randomfolder+"&");
}

function cancleFile() {
    createXmlHttp();
    xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
	        document.uploadForm.submit();
	        document.getElementById("upload").outerHTML=document.getElementById("upload").outerHTML.replace(/(value=\").+\"/i,"$1\"");
	        document.getElementById("cleanbutton").disabled="";
		    document.getElementById("bester").disabled="";
		    document.getElementById("saveBnt").disabled="";
		    document.getElementById("resetBnt").disabled="";
		    document.getElementById("canclebutton").disabled="true";
		    document.getElementById("percentText").innerHTML = "";
	        addFile();	    
	    }    
    };
    xmlHttp.open("POST", "upload-ajaxRequest.action", true);
    xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlHttp.send("ajaxControl=cancleFile&randomfolder="+randomfolder+"&datepath="+datepath+"&timestamp="+new Date().getTime());
}

function delFile(pathRoot,pathRandoom,pathName) {
	
	if(i==1){
		alert('文件上传中!');
		return;
	}
	if(!confirm('确定删除？')) {
		return;
	}
    createXmlHttp();
    xmlHttp.onreadystatechange = function(){
	    if (xmlHttp.readyState == 4) {
	        document.getElementById("uploads").innerHTML = xmlHttp.responseText;
	        document.getElementById("upload").outerHTML=document.getElementById("upload").outerHTML.replace(/(value=\").+\"/i,"$1\"");
	        document.getElementById("cleanbutton").disabled="";
		    document.getElementById("bester").disabled="";
		    document.getElementById("saveBnt").disabled="";
		    document.getElementById("resetBnt").disabled="";
		    addFile();
	    }    
    };
    xmlHttp.open("POST", "upload-ajaxRequest.action", true);
    xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlHttp.send("ajaxControl=delFile&randomfolder="+randomfolder+"&datepath="+datepath+"&pathroot="+pathRoot+"&pathrandom="+pathRandoom+"&pathname="+pathName+"&timestamp=" + new Date().getTime());
}

function sendRequest(params) {
    createXmlHttp();
    xmlHttp.onreadystatechange = showUploadFile;
    xmlHttp.open("POST", "upload-ajaxRequest.action", true);
    xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlHttp.send(params+"datepath="+datepath+"&timestamp="+new Date().getTime());
}
function showUploadFile() {
    if (xmlHttp.readyState == 4) {
        document.getElementById("uploads").innerHTML = xmlHttp.responseText;
        document.getElementById("upload").outerHTML=document.getElementById("upload").outerHTML.replace(/(value=\").+\"/i,"$1\"");
        document.getElementById("cleanbutton").disabled="";
	    document.getElementById("bester").disabled="";
	    document.getElementById("saveBnt").disabled="";
	    document.getElementById("resetBnt").disabled="";
   }
}

var i = 0;

function startStatusCheck(){
	i=1;
    createXmlHttp();
    var attachnum = document.getElementById("attachnum").value;
    document.getElementById("canclebutton").disabled="";
    xmlHttp.onreadystatechange = showUploadFilePercent;
    xmlHttp.open("POST", "upload-returnValue.action", true);
    xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlHttp.send("randomfolder="+randomfolder+"&attachNum="+attachnum+"&timestamp=" + new Date().getTime());
}

String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

function showUploadFilePercent() {
    if(xmlHttp.readyState == 4) {
		document.getElementById("percentText").innerHTML = xmlHttp.responseText;
		if(xmlHttp.responseText.trim()!=''){
			if(
				document.getElementById("messageInfo").value!='null' && document.getElementById("messageInfo").value!=''){
				document.getElementById("upload").outerHTML=document.getElementById("upload").outerHTML.replace(/(value=\").+\"/i,"$1\"");
				document.uploadForm.submit();
				alert(document.getElementById("messageInfo").value);
				document.getElementById("cleanbutton").disabled="";
				document.getElementById("bester").disabled="";
				document.getElementById("saveBnt").disabled="";
				document.getElementById("resetBnt").disabled="";
				document.getElementById("canclebutton").disabled="true";
				i=0;
				return;
			}else{
				document.getElementById("cleanbutton").disabled="true";
	            document.getElementById("bester").disabled="true";
	            document.getElementById("saveBnt").disabled="true";
	            document.getElementById("resetBnt").disabled="true";
                startStatusCheck();
			}
		}else{
			addFile();
			document.getElementById("canclebutton").disabled="true";
		}
	}
}